home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_08 / saks / strtst3c.cpp < prev   
Encoding:
C/C++ Source or Header  |  1994-06-12  |  1.6 KB  |  80 lines

  1. Listing 6 - A test program for the strq wrapper class with an output 
  2. operator
  3.  
  4. //
  5. // strtst3c.cpp - test genq3 using str elements
  6. //
  7.  
  8. #include <iostream.h>
  9.  
  10. #include "showheap.h"
  11. #include "strq3.h"
  12.  
  13. ostream *os_ptr;
  14.  
  15. void print_str(void *e)
  16.     {
  17.     *os_ptr << ' ' << *(str *)e;
  18.     }
  19.  
  20. ostream &operator<<(ostream &os, strq &q)
  21.     {
  22.     os_ptr = &os;
  23.     q.apply(print_str);
  24.     return os;
  25.     }
  26.  
  27. #define DIM(a) (sizeof(a)/sizeof(a[0]))
  28.  
  29. void test()
  30.     {
  31.     char c;
  32.     size_t qn;
  33.     str qe;
  34.     strq q[4];
  35.     while (cin >> c)
  36.         {
  37.         showheap();
  38.         if (c == 'q')
  39.             break;
  40.         if (c == 'a')
  41.             {
  42.             cin >> qn >> qe;
  43.             if (qn >= DIM(q))
  44.                 cout << "no such queue\n";
  45.             else
  46.                 q[qn].append(qe);
  47.             }
  48.         else if (c == 'c')
  49.             {
  50.             cin >> qn;
  51.             if (qn >= DIM(q))
  52.                 cout << "no such queue\n";
  53.             else
  54.                 q[qn].clear();
  55.             }
  56.         else if (c == 'r')
  57.             {
  58.             cin >> qn;
  59.             if (qn >= DIM(q))
  60.                 cout << "no such queue\n";
  61.             else if (q[qn].remove(qe))
  62.                 cout << "removed " << qe << '\n';
  63.             else
  64.                 cout << "q[" << qn << "] is empty\n";
  65.             }
  66.         else
  67.             continue;
  68.         for (size_t i = 0; i < DIM(q); ++i)
  69.             cout << i << ':' << q[i] << '\n';
  70.         }
  71.     }
  72.  
  73. int main()
  74.     {
  75.     showheap();
  76.     test();
  77.     showheap();
  78.     return 0;
  79.     }
  80.